/**
 * 功能描述 
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper")
	var Form = require("form");
	var store =require("store");
	var activeUser = store.get("user");
    var instance = {
        initComponent: function (params,widget) {
        	var form = new Form({
        	 parentNode:".J-form",
	  			 model:{
					 id:"form",
		       		 saveaction : function(){
		       			var data = form.getData();
		       			aw.saveOrUpdate("api/cardispatch/savecarbill",data,function(data){
		       				widget.show([".J-grid"]).hide([".J-form"]);
							widget.get("subnav").hide(["return"]).show(["search","add","carDate","status"]);
							widget.get("grid").refresh();
		       			});
		       		 },
		       		 cancelaction : function(){       			 
			       			widget.show([".J-grid"]).hide([".J-form"]);
							widget.get("subnav").hide(["return"]).show(["search","add","carDate","status"]);
							widget.get("grid").refresh();
		       		 },
					items:[{
						name:"oddCode",
						label:"单号",
						readonly:true,
						defaultValue: moment().format("YYMMDD")+parseInt(Math.random()*1000)
					},{
						name:"groupReserveN",
						label:"组团单位"
					},{
						name:"destination",
						label:"目的地"
					},{
						name:"checkInDate",
						label:"入住日期"
					},{
						name:"checkOutDate",
						label:"退住日期"
					},/*{
						name:"signUpNum",
						label:"报名人数"
					},*/{
						name:"unitsCar",
						label:"本单位车辆",
						type:"radio",
	        			list:[{
	        				key:true,
	        				value:"是"
	        				},{
	        				key:false,
	        				value:"否"
	        			}],
	        			defaultValue:"true",
	        			events:{
	        				"change":function(){
	        					var form=widget.get("form");	
	        					if(form.getValue("unitsCar")){
	        						form.hide(["companyName","carModel","driverName"]);
	        						form.show(["car","carNumber","driver"]);
	        					}else{
	        						form.hide(["car","carNumber","driver"]);
	        						form.show(["companyName","carModel","driverName"]);
	        					}
	        				}
	        			}
					},{
			        	name:"car",
			        	label:"车辆型号",
			        	type:"select",
			        	url:"api/carfile/query",
			        	key:"pkCar",
			        	value:"carType",
			        	validate: ["required"],
			        	params:function(){
			        		return {
			        			"carStatus":"Free",
			        		}
			        	},
			        	events:{
			        		"change":function(){
			        			var pk = form.getValue("car");
			        			var carDate =  form.getData("car");
			        			for(var i=0;i< carDate.length;i++){
			        				if(carDate[i].pkCar==pk){
				        				form.setValue("carNumber",carDate[i].carNumber);
				        			}
			        			}
			        		}
			        	}
			        	
			        },{
			        	name:"carNumber",
			        	label:"车牌号",
			        	validate: ["required"]
			        },{
			        	name:"driver",
			        	label:"司机名称",
			        	type:"select",
			        	url:"api/driverfile/query",
			        	key:"pkDriver",
			        	value:"personalInfo.name",
			        	validate: ["required"],
			        	params:function(){
			        		return {
			        			"driverStatus":"Free",
			        			fetchProperties:"pkDriver,personalInfo.name,personalInfo.phone",
			        		}
			        	},
			        	events:{
			        		"change":function(){
			        			var pk = form.getValue("driver");
			        			var driverData = form.getData("driver");
			        			for(var i = 0; i<driverData.length;i++){
			        				if(driverData[i].pkDriver==pk){
			        					form.setValue("driverPhone",driverData[i].personalInfo.phone);//
			        				}
			        			}
			        		}
			        	}
			        },{
			        	name:"companyName",
			        	label:"公司名称",
			        	type:"text",
			        	validate: ["required"],
						exValidate: function(value) {
                            var reg = /^[\u4e00-\u9fa50-9A-Za-z]{1,20}$/;
                            if (!reg.test(value)) {
                                return "只能输入汉字、字母和数字,最多20位";
                            } else {
                                return true;
                            }
                        }
			        },{
			        	name:"carModel",
			        	label:"车辆型号",
			        	validate: ["required"],
			        },{
			        	name:"driverName",
			        	label:"司机姓名",
			        	validate: ["required"],
			        },{
			        	name:"driverPhone",
			        	label:"司机电话",
			        	validate: ["required"],
			        },{
			        	name:"dispatchDate",
			        	label:"用车时间",
			        	type:"date",
			        	validate: ["required"]
			        },{
			            name: "address",
			            label: "派车地点",
			            validate: ["required"]
			        },{
			        	name:"totalPrice",
			        	label:"总费用(元)",
			        	validate: ["required"]
			        },{
			            name: "description",
			            type: "textarea",
			            label: "备注",
			        },{
			        	name:"version",
			        	type:"hidden"
			        },{
			        	name:"pkCarDispatchBill",
			        	type:"hidden"
			        },{
			        	name:"nonMemGroupReserve",
			        	type:"hidden"
			        },{
			        	name:"groupReserve",
			        	type:"hidden"
			        },{
			        	name:"carStatus",
			        	type:"hidden",
			        	defaultValue:"Initial"
			        }]
	  			 }
           });
        widget.set("form",form);
       }
    };
    module.exports = instance;
});
